package fr.cephb.operonj.core.je;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.SecondaryDatabase;
import com.sleepycat.je.Transaction;

public abstract class SecondaryDB<K,PK,V> extends AbstractDuplicateDB<K,V>
	{
	private SecondaryDatabase database2;
	private PrimaryDB<PK, V> primaryDB;
	public SecondaryDB(
			Environment env,
			Transaction txn,
			String databaseName,
			PrimaryDB<PK, V> primaryDB,
			SecondaryConfig dbConfig
			) throws DatabaseException
		{
		
		if(databaseName==null) databaseName=getDefaultName();
		this.primaryDB=primaryDB;
		this.database2= env.openSecondaryDatabase(txn, databaseName, primaryDB.getDatabase(), dbConfig);
		}
	
	public PrimaryDB<PK,V> getPrimaryDB()
		{
		return this.primaryDB;
		}
	
	public SecondaryDatabase getSecondaryDatabase()
		{
		return database2;
		}
	
	@Override
	public final Database getDatabase()
		{
		return getSecondaryDatabase();
		}

	@Override
	public abstract TupleBinding<K> getKeyBinding();

	@Override
	public TupleBinding<V> getValueBinding()
		{
		return getPrimaryDB().getValueBinding();
		}
	}
